﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace SistemaComprasVentaYICHAN
{
    public class ConexionBD
    {
        public static OleDbConnection con;

        public ConexionBD()
        {
            con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YICHAN_DataBase.mdb");
            con.Open();
        }

        //string query " <<SQL query>> ";
        public static int EjecutarNonQuery(string query)
        {
            //OleDbTransaction trans = con.BeginTransaction();
            //try
            //{
            OleDbCommand cmd = new OleDbCommand(query, con);

            //cmd.Transaction = trans;
            return cmd.ExecuteNonQuery(); // insert, update, delete
            //trans.Commit();
            //return 1;
            //}
            //catch (Exception ex)
            //{
            //    trans.Rollback();
            //  return 0;
            //}
        }

        public static Object EjecutarEscalar(string query)
        {
            OleDbCommand cmd = new OleDbCommand(query, con);
            return cmd.ExecuteScalar(); // select count(*) from table
        }

        public static OleDbDataReader EjecutarReader(string query)
        {
            OleDbCommand cmd = new OleDbCommand(query, con);
            OleDbDataReader reader = cmd.ExecuteReader();
            return reader; // select * from table

            //while (reader.Read())
            //{
            //   Console.WriteLine(reader[0].ToString());
            //}
            //reader.Close();  para leer los campos
        }

        public static void CerrarConexion()
        {
            con.Close(); //siempre se abre y se cierra
        }
    }
}

